/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.guix;

import edu.yonsei.iwb.sdt.datastructure.tree.DNode;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.TitledBorder;
//import kr.ac.kaist.swrc.jhannanum.comm.Eojeol;

/**
 *
 * @author DK
 */
public class GDLearningPanel extends javax.swing.JPanel implements ActionListener, ItemListener {
    
    public JPanel ontologyInfoPanel;
    private JPanel runOptionInfoPanel;
    private JScrollPane outputScrollPane;
    private JSplitPane learningInfoSplitPane;
    private JSplitPane runOptionInfoSplitPane;
    public JSplitPane ontInfobtwloptANDtreeSplitPane;
    private JSplitPane treeANDoutputTextSplitPane;
    public JTextArea outputTextArea;
    private String ontology;
    private JRadioButton isTargetProperty;
    private JRadioButton isTargetInstance;
    private Controller ctrl;
    public SDTViewPanel svp;
    public TargetInstanceViewFrame oivp;
    private GraphDataLearner gdl;
    
    public GDLearningPanel(){
        
    }

    public GDLearningPanel(GraphDataLearner gdl) {

//        controller = new Controller();
        

        learningInfoSplitPane = new JSplitPane();
        ontInfobtwloptANDtreeSplitPane = new JSplitPane();
        treeANDoutputTextSplitPane = new JSplitPane();
//        maAndNerSplitPane = new JSplitPane();
        ontologyInfoPanel = new JPanel();
        
        outputScrollPane = new JScrollPane();
        outputTextArea = new JTextArea();
                
        runOptionInfoPanel = new JPanel();
        runOptionInfoSplitPane = new JSplitPane();
        
        this.gdl = gdl;
        this.ctrl = gdl.getController();
        this.svp = new SDTViewPanel(this);        
        this.oivp = new TargetInstanceViewFrame(this);
        
        ontologyInfoPanel.setVisible(false);
//        runOptionInfoPanel.setVisible(false);
        outputTextArea.setVisible(false);

                
//        this.setPreferredSize(new Dimension(1000, 1000));

//        System.out.println("?????????????????????????");
        /**
         * Maing Frame 화면 분할
         */
        runOptionInfoSplitPane.setBorder(null);
//        runOptionInfoSplitPane.setVisible(false);
        runOptionInfoSplitPane.setDividerLocation(600);
        runOptionInfoSplitPane.setDividerSize(0);
        runOptionInfoSplitPane.setOrientation(javax.swing.JSplitPane.HORIZONTAL_SPLIT);

        runOptionInfoSplitPane.setLeftComponent(new LearningInfoViewPanel(this));
        runOptionInfoSplitPane.setRightComponent(new LearningOptionViewPanel(this));

        learningInfoSplitPane.setBorder(null);
        learningInfoSplitPane.setDividerLocation(150);
        learningInfoSplitPane.setDividerSize(0);
        learningInfoSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
//        learningInfoSplitPane.setVisible(false);

        JScrollPane tscroll = new JScrollPane(this.svp, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        learningInfoSplitPane.setTopComponent(runOptionInfoSplitPane);
        learningInfoSplitPane.setBottomComponent(treeANDoutputTextSplitPane);//setRightComponent

        treeANDoutputTextSplitPane.setDividerLocation(650);
        treeANDoutputTextSplitPane.setDividerSize(7);
        treeANDoutputTextSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        treeANDoutputTextSplitPane.setOneTouchExpandable(false);

        treeANDoutputTextSplitPane.setTopComponent(tscroll);
        treeANDoutputTextSplitPane.setBottomComponent(this.outputScrollPane);

        /**
         * 온톨로지 정보 패널
         */
        runOptionInfoPanel.setBorder(new TitledBorder("Learning Option"));
        outputTextArea.setColumns(20);
        outputTextArea.setRows(5);
        outputScrollPane.setViewportView(outputTextArea);
                
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(learningInfoSplitPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(learningInfoSplitPane, GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
        );
        this.setVisible(true);

    }

    public void ontologyLoaded() {
        CurrentMainFrameStatus.ONTOLOGY_LOADED = true;
    }

    public String getOntology() {
        return this.ontology;
    }

    public Controller getController() {
        return this.ctrl;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {

        // Target Property를 사용할 것인지 Target Instance를 지정할 것인지 결정

        Object ob1 = e.getSource();
        if (e.getStateChange() == ItemEvent.SELECTED) {
            if (ob1 == isTargetProperty) {
                this.oivp.setVisible(false);
//                ontInfobtwloptANDtreeSplitPane.setDividerLocation(0);
                outputTextArea.append("Select Target Property\n");
            } else if (ob1 == isTargetInstance) {
                this.oivp.setVisible(true);
                ontInfobtwloptANDtreeSplitPane.setDividerLocation(800);

//                ontInfobtwloptANDtreeSplitPane.setDividerLocation(800);
                outputTextArea.append("Select Target Instance\n");
            }


        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Object o = ae.getSource();
        

    }
//
    public void main(DNode node) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Windows Classic".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(GDLearningPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        final JFrame frame = new JFrame();
        frame.setSize(800, 800);
        frame.add(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
//                int confirm = JOptionPane.showOptionDialog(frame, "Really Exit?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
//                if(confirm == 0){
                frame.dispose();
//                System.exit(0);
//                }
            }
        };
        frame.addWindowListener(l);
        
        this.svp.showGraph(node);
        
        
    }
    
    
    
    
}
